/******************************************************************************
 * Copyright (C), 2014, BeeIoT Tech. Co., Ltd. 
 * FileName:        at_api.c
 * Author:          lvxin 
 * Version :        1.0
 * Date:            2014-10-23 10:23:41
 * Description:     AT指令API定义
 * Function List:   1. at_api_init()    --初始化API列表,将列表提供给at_service
 * History:
 * <author>     <time>                <version >  <desc> 
 * lvxin        2014-10-23 10:23:41   1.0         build this moudle 
 *****************************************************************************/

/******************************************************************************
 *  *INCLUDES
 *  */
#include <stdio.h>
#include <stdlib.h>
#include "at_service.h"
#include "at_base.h"
#include "SIM900A.h"
#include "smset.h"
#include "pdu.h"
#include "conv.h"
#include "setup.h"


extern void alarm_demo(void);
/******************************************************************************
 *  *PROFILE CALLBACKS(DECLARES)
 *  */
static int CCLK_api (int argc, char *argv[]);
static int SPWM_api (int argc, char *argv[]);

/******************************************************************************
 *  *FUNCTION DECLARES
 *  */
static int app_dp   (int argc, char *argv[]);
static int app_on   (int argc, char *argv[]);
static int app_off  (int argc, char *argv[]);
static int app_at   (int argc, char *argv[]);
static int app_con  (int argc, char *argv[]);
static int app_shut (int argc, char *argv[]);
static int app_send (int argc, char *argv[]);
static int app_stat (int argc, char *argv[]);
static int app_close(int argc, char *argv[]);
static int app_test (int argc, char *argv[]);
static int app_year (int argc, char *argv[]);
static int app_time (int argc, char *argv[]);
static int app_settime(int argc, char *argv[]);
static int app_sm(int argc, char *argv[]);
static int app_read(int argc, char *argv[]);
static int app_qd(int argc, char *argv[]);

/******************************************************************************
 *  *PUBLIC FUNCTIONS
 *  */
//AT指令的API列表
static at_api_list_t api_list[]={
    {"CCLK", CCLK_api},
    {"+SPWM", SPWM_api},
    {"DP",     app_dp   },
    {"ON",     app_on   },
    {"OFF",    app_off  },
    {"AT",     app_at   },
    {"CON",    app_con  },
    {"SEND",   app_send },
    {"STAT",   app_stat },
    {"CLOSE",  app_close},
    {"TEST",   app_test },
    {"YEAR",   app_year },
    {"SHUT",   app_shut },
    {"TIME",   app_time },
    {"SETTIME",   app_settime },
    {"SM",   app_sm },
    {"READ",   app_read },
    {"QD",   app_qd },
};

/******************************************************************************
 * Function:        at_api_init()
 * Description:     初始化API接口
 * Calls:           at_api_list_register()
 * Called By:       at_service_init()
 * Input:            
 * Return:          void
 * Others:           
 *****************************************************************************/
void at_api_init()
{
    at_api_list_register (api_list, sizeof(api_list)/sizeof(api_list[0]));
}

static int CCLK_api (int argc, char *argv[])
{
    int i;
    at_base_printf ("参数列表：\n");
    for (i = 0; i < argc; i ++)
    {
        at_base_printf ("[%d]%s\n", argc, argv[i]);
    }
    return 0;
}

static int SPWM_api (int argc, char *argv[])
{
    int i;
    at_base_printf ("参数列表：\n");
    for (i = 0; i < argc; i ++)
    {
        at_base_printf ("[%d]%s\n", argc, argv[i]);
    }
    at_base_printf ("%s", "hello");
    return 0;
}




/******************************************************************************
 *  *LOCAL FUNCTIONS
 *  */


static int app_dp(int argc, char *argv[])
{
    /*sm_inner_cmd_push_fifo (4, 5); //通过TCP发送余额*/
    alarm_demo();
    
    /*DBG_PRINTF("报警发送结果：%d\n>>>>>>>>>>>>>>>\n", SIM900A_gprs_send_tcp(0));*/
    DBG_PRINTF("报警发送结果：%d\n>>>>>>>>>>>>>>>\n", SIM900A_run_dp_alarm ());
    return 0;
}

static int app_on(int argc, char *argv[])
{
    // type : 0 --大棚报警
    // type : 1 --来电报警
    // type : 2 --断电报警
    // type : 3 --余额报警
    DBG_PRINTF("报警发送结果：%d\n>>>>>>>>>>>>>>>\n", SIM900A_gprs_send_tcp(1));
    return 0;
}

static int app_off(int argc, char *argv[])
{
    // type : 0 --大棚报警
    // type : 1 --来电报警
    // type : 2 --断电报警
    // type : 3 --余额报警
    /*SIM900A_gprs_send_tcp(2);*/
    DBG_PRINTF("报警发送结果：%d\n>>>>>>>>>>>>>>>\n", SIM900A_gprs_send_tcp(2));
    return 0;
}

static int app_at(int argc, char *argv[])
{
    SIM900A_perform_formatted(3000, NULL, NULL, "%s\r\n", "AT");
    return 0;
}

static int app_con(int argc, char *argv[])
{
    DBG_PRINTF("TCP连接状态：%d\n",gprs_net_connect("tcp", "www.beeiot.com", 2015));
    return 0;
}

static int app_send(int argc, char *argv[])
{
    gprs_net_send_formatted ("{\"param_array\": {\"ID\":\"000000000000\",\"stat\": 1 },\"class\": \"DP\",\"method\": \"powerStat\"}\r\n\x1a"); //电源接通
    /*gprs_net_send_formatted ("{\"param_array\": {\"ID\":\"000000000000\",\"stat\": 0 },\"class\": \"DP\",\"method\": \"powerStat\"}\r\n\x1a"); //电源断开*/
    return 0;
}

static int app_stat(int argc, char *argv[])
{
    printf ("%d\n",gprs_net_get_stat());
    return 0;
}

static int app_close(int argc, char *argv[])
{
    SIM900A_run ("AT+CIPCLOSE=1\r\n", 3000, NULL, NULL);
    return 0;
}

static int app_test(int argc, char *argv[])
{
    SIM900A_reboot();
    return 0;
}

static int app_year(int argc, char *argv[])
{
    printf ("日期：%02d\n", SIM900A_get_monthday());
    printf ("年份：%02d\n", SIM900A_get_year());
    
    return 0;
}
static int app_shut(int argc, char *argv[])
{
    gprs_net_shut();
    return 0;
}

static int app_time(int argc, char *argv[])
{
    uint16_t time;
    /*int i;*/
    /*for (i = 0; i < 100; i ++)*/
    {
        time = SIM900A_get_timestamp();
        DBG_PRINTF ("当前时间：%02d:%02d\n", time >> 8, time & 0xff);
    }
    
    return 0;
}

static int app_settime(int argc, char *argv[])
{
    SIM900A_set_time(0x1223);
    return 0;
}

extern option_t  g_sys_options; //存储全局的系统设置
static int app_sm(int argc, char *argv[])
{
    if (argc > 0)
    {
        printf ("[argc]%d\t[argv]%s\n", argc, argv[0]);
    }
    /*printf ("[发送余额ret]%d\n", SIM900A_gprs_send_tcp(3));*/
    return 0;
}

static int app_read(int argc, char *argv[])
{
    char buffer[50] = {0};
    printf ("读取网络数据：[reg]%d\t[cont]%s\n", gprs_net_read(15000, buffer, 50), buffer); 
    return 0;
}



/*static void memdump (void *first, size_t len)*/
/*{*/
    /*int line = 0;*/
    /*int i = 0;*/
    /*printf ("\n\033[1;33mADDRESS : ");*/
    /*for (i = 0; i < 16; i ++)*/
    /*{*/
        /*printf ("%02X ", i);*/
    /*}*/
    /*printf ("\033[0m");*/
    /*for (i = 0; i < len; i ++)*/
    /*{*/
        /*if (i%16==0)*/
        /*{*/
            /*printf ("\033[1;33m\n%08X: \033[0m", (line ++)*16);*/
        /*}*/
        /*printf ("%02X ", *((unsigned char *)first + i));*/
    /*}*/
    /*printf ("\n========================================\n");*/
/*}*/
static int app_qd(int argc, char *argv[])
{
    char *str = "你好！1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 qwertyuiopasdfghjklzxcvbnm";
    /*char *str = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";*/
    /*sm_option_view_dp_info(atoi(argv[0]));*/
    /*pdu_spt_print_pdu_ret_t tmp;*/
    /*tmp.pos = str;*/
    /*do*/
    /*{*/
    /*tmp = pdu_spt_print_utf8_to_pdu(tmp.pos, strlen (tmp.pos), PDU_70WORDS_LIMIT | PDU_PRINT);*/
    /*printf ("[pdulen]%d\n",tmp.pdu_len);*/
    /*printf ("[pos]%p\n",tmp.pos);*/
    /*printf ("[remain]%d\n",tmp.remain_bytes);*/
    /*printf ("[utf8len]%d\n",strlen(str));*/
    /*}*/
    /*while (tmp.pos != NULL);*/
   /*printf("[字符数]%d\n", strlen (str));*/
   /*SMS_printf("%s\n", argv[0]);*/
   /*SMS_flush();*/
   /*printf("[总共处理的字符数]%d\n", SMS_printf("%s\n", str));*/
    /*SMS_connect ("15040002193", 11);*/
    /*alarm_demo();*/
    /*SIM900A_run_dp_alarm ();*/
    /*char *str = "946B003A00200031003200330034003500360037003800390030";*/
    char *ss = strdup(str);
    /*size_t bufflen = strlen(ss);*/
    /*size_t buffsize = strlen(ss);*/
    SMS_connect ("15040002193", 11);
    SMS_printf(ss);
    SMS_flush();
    /*conv_pdu_to_bin (ss, ss, &bufflen);*/
    /*memdump (ss, bufflen);*/
    /*conv_ucs2_to_utf8(ss, bufflen, ss, &buffsize);*/
    /*memdump (ss, buffsize);*/
    /*printf ("[df]%s\n", ss);*/
    free(ss);
    /*SIM900A_send_phone_call_alarm ();*/
    /*SIM900A_send_phone_call_alarm();*/
    return 0;
}
